
#基类

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import time,os


class Base():
    def __init__(self) -> object:
        cap = {
            "platformName": "Android",
            "deviceName": "emulator-5554 ",
            "appPackage": "com.xiachufang",
            "appActivity": "com.xiachufang.activity.home.HomeActivity",
            "noReset": True
        }
        os.system('adb root')
        self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',cap)
        self.driver.implicitly_wait(25)
        sleep(10)
    def loc_ele(self,loc):
        try:
           return self.driver.find_element(*loc)
        except Exception as e:
            print("报错信息",e)
            print("未定位变量值",loc)
    def loc_wait_ele(self,loc):
        try:
           return WebDriverWait(self.driver,15).until(EC.presence_of_element_located(loc))
        except Exception as e:
            print("报错信息",e)
            print("未定位变量值",loc)
    def loc_ui_ele(self,loc):
        try:
           return self.driver.find_element_by_android_uiautomator(loc)
        except Exception as e:
            print("报错信息",e)
            print("未定位变量值",loc)
    def quit(self):
        self.driver.quit()
    def haudong(self,q,w,e,r):
        self.driver.swipe(q,w,e,r,500)
    def qingqiao(self,q,w):
        self.driver.swipe([(q,w)],500)
    def get_data_key(self,loc,key):
        if type(loc)==str:
           a=self.loc_ui_ele(loc)
        else:
           a=self.loc_ele(loc)
        return a.get_attribute(key)
    def jieping(self,filename):
        t=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot(t+filename)

